#include "mysql_cd.c"

void cdTable(MYSQL *mysql, int userid, int peerfd, char* pwd) {
    int len = strlen(pwd);
    char* gtfilename = {0};
    int j = 0;
    int k = 0;
    int pidw = -1; //开始根节点pid=-1
    int ID[256] = 0;
    char rule[1024] = {0};
    for (int i = 0; i < len; i++) {
        if (pwd[i] == '/') {
            gtfilename = substring(pwd, k, i-k);
            k = i+1;
            printf("%s", gtfilename);
            sprintf(rule, "filename = \"%s\" and pid = %d", gtfilename, pidw); //开始根节点pid=-1
            char *retval = searchTableData(mysql, "id", "virtualfile", rule);
            if (retval == NULL) {
                sendMsg(peerfd, err);
                return;
            }
            ID[j] = atoi(retval);
            pidw = ID[j];
            userCheck(peerfd, userid, pidw);
            typeCheck(peerfd, pidw);
            j ++;
            memset(&rule, 0, 1024);
        }
    }
    gtfilename = substring(pwd, k, len-k+1);
    printf("%s\n", gtfilename);
    sprintf(rule, "filename = \"%s\" and pid = %d", gtfilename, pidw);
    char *retval = searchTableData(mysql, "id", "virtualfile", rule);
    if (retval == NULL) {
        sendMsg(peerfd, err);
        return;
    }
    ID[j] = atoi(retval);
    pidw = ID[j];
    userCheck(peerfd, userid, pidw);
    typeCheck(peerfd, pidw);
    sendMsg(peerfd, pwd);
    addTableData(mysql, "user", "pwd", pwd);
}


void typeCheck(int peerfd, int id) {
    char where[1024] = {0};
    sprintf(where, "id = %d", id);
    char *retval = searchTableData(mysql, "type", "virtualfile", where);
    if (retval == NULL) {
        sendMsg(peerfd, err);
        return;
    }
    int type = atoi(retval);
    if (type != 1) {
        sendMsg(peerfd, err);
        return;
    }
}


void userCheck(int peerfd, int userid, int id){
    char where[1024] = {0};
    sprintf(where, "id = %d", id);
    char *retval = searchTableData(mysql, "owner_id", "virtualfile", where);
    if (retval == NULL) {
        char* err = "Wrong dir! Please check!";
        sendMsg(peerfd, err);
        return;
    }
    int ownerid = atoi(retval);
    if (ownerid != userid) {
        char* err = "Wrong dir! Please check!";
        sendMsg(peerfd, err);
        return;
    }
}


int addTableData(MYSQL *mysql, char *Tname, char *field, char *value){
    char sql[SQL_MAX] = {0};
    snprintf(sql, SQL_MAX, "INSERT INTO %s (%s) values (%s)", Tname, field, value);
    //printf("插入sql语句：%s\n", sql);
    int ret = mysql_query(mysql, sql);
    if (ret != 0) {
        printf("插入表数据失败！失败原因：%s\n",
               mysql_error(mysql));
        return -1;
    }
    //printf("插入表数据成功！\n");
    return 0;
}


char * searchTableData(MYSQL *mysql, char *column, char *Tname, char *where) {
    char sql[SQL_MAX] = {0};
    snprintf(sql, SQL_MAX, "SELECT %s FROM %s WHERE (%s)", column, Tname, where);
    //printf("查询sql语句：%s\n", sql);
    int ret = mysql_query(mysql, sql);
    if (ret) {
        printf("查询表数据失败！失败原因：%s\n",
               mysql_error(mysql));
        return NULL;
    }
    //printf("查询表数据成功！\n");
    MYSQL_RES *result = mysql_store_result(mysql);
    if(result == NULL){
        printf("error query: %s\n", mysql_error(mysql));
        return NULL;
    }
    int rows = mysql_num_rows(result);
    int cols = mysql_num_fields(result);
    //printf("rows:%d, cols:%d\n", rows, cols);

    MYSQL_ROW row;
    char *back = (char *)calloc(1,1024);
    while((row = mysql_fetch_row(result)) != NULL){
        for(int i = 0; i < cols; ++i){
            char temp[1024] = {0};
            sprintf(temp, "%s", row[i]);
            //printf("temp:%s\n", temp);
            strcat(back, temp);
            //printf("back:%s", back);
        }
        //printf("\n");
    }
    mysql_free_result(result);
    return back;
}


char* substring(char* ch,int pos,int length){
    //定义字符指针 指向传递进来的ch地址
    char* pch=ch;
    //通过calloc来分配一个length长度的字符数组，返回的是字符指针。
    char* subch=(char*)calloc(sizeof(char),length+1);
    int i;
    //只有在C99下for循环中才可以声明变量，这里写在外面，提高兼容性。
    pch=pch+pos;
    //是pch指针指向pos位置。
    for(i=0;i<length;i++)
    {
        subch[i]=*(pch++);
        //循环遍历赋值数组。
    }
    subch[length]='\0';//加上字符串结束符。
    return subch;       //返回分配的字符数组地址。
}
